Write
The Write command writes data to a specified file, beginning at the file mark. It is one of several commands provided by the Read/Write Commands scripting addition. For more information about these commands, see "Using Read/Write Commands,".SYNTAX
write dataToWrite to referenceToFile [ for bytesToWrite ] ¬ [ starting at startingByte ]PARAMETERS
- dataToWrite
- The data to be written. The format of the data should match the file type.
Class: (varies among applications)
- referenceToFile
A reference of the formfile nameString
oralias nameString
, or a file reference number previously obtained with the Open for Access command (see "Notes").
Class: Reference or integer
- bytesToWrite
- The number of bytes to write. The Write command returns an error if you pass a negative number in this parameter.
Class: Integer
- startingByte
- Specifies the offset of the byte at which to begin writing. A positive integer indicates the offset from the beginning of the file, and a negative integer indicates the offset from the end of the file.
Class: IntegerRESULT
NoneEXAMPLES
This example writes "abcde" to the file MyFile.
write "abcde" to file "Hard Disk:MyFile"The next example returns an error because fewer bytes are specified for the dataToWrite parameter than are specified for thefor
bytesToWrite parameter:
write "abcde" to file "Hard Disk:MyFile" for 8If the data to write is longer than the bytes specified by the bytesToWrite parameter, the Write command truncates the data. For example, this Write command writes the number 5 to the file MyFile as a short integer (2 bytes) rather than an integer (4 bytes):
write 5 to file "Hard Disk:MyFile" for 2The next example specifies a negative value for the startingByte parameter. It writes the number 5 as a short integer starting at the 8th byte before the end of the file.
write 5 to file "Hard Disk:MyFile" starting at -8 for 2NOTES
The file mark is a marker used by the File Manager that indicates the byte at which the Read command expects to begin reading data. By default, the file mark is the first byte of the file. The Write command begins writing at the current file mark and sets the file mark to the byte after the last byte written. The Read command can also reset the file mark.To set the file mark without reading or writing data, write a string of zero length to the byte to which you want to set the mark. For example, this script sets the file mark for the file specified by fileRefNum to the fourth byte in
the file:
write "" to fileRefNum starting at 4To specify the name (nameString) of a file, use a string of the form"Disk:Folder1:
as described in Chapter 5, "Objects and References,"
Folder2:...:Filename"
of the AppleScript Language Guide. If you specify only the name of the file (Filename) instead of its entire pathname, AppleScript attempts to find the file in the current directoryThe Write command attempts to match a reference to a file or an alias with a file previously opened with the Open for Access command. If a match is found, it simply writes the specified data. If no match is found but the file can be located on disk, the Write command opens the file, writes the specified data, then closes the file. The file mark for a file opened in this fashion is always
at the beginning of the file. If the file cannot be found at all, the Write command returns an error.If you specify a file reference number previously obtained with the Open for Access command, the Write command writes the specified data immediately.
ERRORS